Contrib Colormaps¶
Collection of user-contributed colormaps¶
contrib_colormaps is a collection of user-contributed colormaps for use with Python plotting programs like Bokeh, Matplotlib, HoloViews, and Datashader.
View Colormap Swatches¶
For ease of use, we provide minimal plotting commands for use with contrib_colormaps. To use these plotting commands, you'll need to install HoloViews.
NOTE: HoloViews is not required to install or use contrib_colormaps, it is only required when using the plotting helper functions.
from contrib_colormaps.plotting import swatch
import holoviews as hv
hv.extension('matplotlib', logo=False)
swatch('sample')
hv.extension('bokeh', logo=False)
swatch('sample')
Accessing the colormaps¶
After importing contrib_colormaps
as cc
, the colormaps will be available for use in different forms. This library should have at least one such form convenient for any particular application. This library generates two versions of each colormap:
- A Bokeh-style palette, i.e., a Python list of RGB colors as hex strings, like
['#000000', ..., '#ffffff']
- A Matplotlib
LinearSegmentedColormap
, i.e., a list of RGBA tuples, like[[0.0,0.0,0.0], ..., [1.0,1.0,1.0]])
Matplotlib¶
import numpy as np
import contrib_colormaps as cc
import matplotlib.pyplot as plt
%matplotlib inline
x = np.arange(-10,11)
fig, ax = plt.subplots(figsize=(5,2.5))
im = ax.scatter(x, -(x**2), c=(x**2), s=40, cmap=cc.cm.sample)
fig.colorbar(im)
plt.show()
Bokeh¶
import numpy as np
import contrib_colormaps as cc
from bokeh.plotting import output_notebook, figure, show
from bokeh.transform import linear_cmap
from bokeh.models import ColorBar, LinearColorMapper
output_notebook()
p = figure(plot_width=300,plot_height=150)
x = np.arange(-10, 11)
y = -(x**2)
source = {'x': x, 'y': y, 'c': y}
color_mapper = LinearColorMapper(palette=cc.b_sample, low=-100, high=0)
color_bar = ColorBar(color_mapper=color_mapper, width=10, location=(0, 0))
p.circle(source=source, x='x', y='y', color=linear_cmap('c', cc.b_sample, -100, 0), size=10)
p.add_layout(color_bar, 'right')
show(p)
The Bokeh-compatible palettes are provided as attributes in the contrib_colormaps
namespace, with long names prefixed with b_
. E.g. rainforest
can be accessed as cc.b_sample
. These names should tab complete once cc
has been imported. Because Bokeh palettes are just Python lists, you can always reverse them using normal Python syntax, e.g. list(reversed(cc.b_sample))
, or use subsets of them with slice notation, e.g. cc.b_sample[25:]
. If you want to access the palettes by string name, they are also collected into a dictionary named palette
, so you can use cc.palette["sample"]
or cc.palette.sample
; whichever is more convenient.
The Matplotlib colormaps are also provided as tab-completable attributes, but consistently with a prefix m_
, e.g. cc.m_sample
. Already reversed versions are also available, as cc.m_sample_r
. The same colormaps are also registered with matplotlib's string-based dictionary with the prefix cc_
, making them available by name within various matplotlib functions (e.g. cc_sample
, cc_sample_r
). Finally, if you want to access the colormaps by string name without using Matplotlib's registry, they are also stored in the cc.cm
dictionary, e.g. cc.cm["sample"]
or cc.cm["sample_r"]
.
Here we show importing sample and printing the first 5 colors.
cc.b_sample[:5]
[cc.m_sample(i) for i in range(5)]
Contributing¶
To contribute a new colormap, see the contributing section of the README. To learn more about how to use the various colormaps and what each is for, see the colormaps section.